<template>
  <div>
    <el-dialog
      :title="dataForm.stateExplain"
      :visible.sync="dialogVisible"
      width="80%"
    >
      <el-row>
        <el-col :span="24">
          <el-form
            :model="dataForm"
            label-width="120px"
            label-position="right"
            :rules="dataRule"
          >
            <div class="rwfqnew">
              <el-row>
                <el-col :span="24">
                  <el-col :span="8">
                    <el-form-item label="发起名称" prop="faqiName">
                      <el-input
                        disabled
                        v-model="dataForm.faqiName"
                        placeholder="发起名称"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="16">
                    <el-form-item label="任务有效期" required>
                      <el-col :span="11">
                        <el-date-picker
                          v-model="dataForm.rwYxqq"
                          type="date"
                          disabled
                          value-format="yyyy-MM-dd"
                          prop="rwYxqq"
                          placeholder="开始日期"
                          style="width: 100%"
                        ></el-date-picker>
                      </el-col>
                      <el-col :span="2">
                        <div class="text-center">至</div>
                      </el-col>
                      <el-col :span="11">
                        <el-date-picker
                          disabled
                          v-model="dataForm.rwYxqz"
                          type="date"
                          value-format="yyyy-MM-dd"
                          format="yyyy-MM-dd"
                          prop="rwYxqz"
                          placeholder="结束日期"
                          style="width: 100%"
                        ></el-date-picker>
                      </el-col>
                    </el-form-item>
                  </el-col>
                </el-col>
                <el-col :span="24">
                  <el-col :span="8">
                    <el-form-item label="填报结束日期" prop="signDate">
                      <el-date-picker
                        @change="changeHead(dataForm.signDate,'signDate',dataForm.id)"
                        v-model="dataForm.signDate"
                        type="date"
                        :disabled="dataForm.state != 0"
                        value-format="yyyy-MM-dd"
                        format="yyyy-MM-dd"
                        placeholder="填报结束日期"
                        style="width: 100%"
                      ></el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="模板名称" prop="templateId">
                      <el-select
                        @change="changeHead(dataForm.templateId,'templateId',dataForm.id)"
                        v-model="dataForm.templateId"
                        placeholder="模板名称"
                        class="xiaLa"
                        style="width: 107%"
                        :disabled="dataForm.state != 0"
                      >
                        <el-option
                          v-for="(item, index) in categoryList"
                          :key="index"
                          :label="item.templateName"
                          :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="发起类型" prop="userLx">
                      <el-select
                        @change="changeHead(dataForm.userLx,'userLx',dataForm.id)"
                        :disabled="dataForm.state != 0"
                        v-model="dataForm.userLx"
                        placeholder="发起类型"
                        class="xiaLa"
                        style="width: 100%"
                      >
                        <el-option
                          v-for="(item, index) in dataForm.typeList"
                          :key="index"
                          :label="item.name"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-col>
                <el-col>
                  <el-col :span="24">
                    <el-button
                      type="text"
                      style="margin-left: 30px"
                      icon="el-icon-user-solid"
                    ></el-button>
                    被考核人：{{ dataForm.ryName }}
                  </el-col>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </el-col>
      </el-row>
      <div style="margin-top: 10px; text-align: right">
        <el-button
          type="primary"
          plain
          @click="rejectedBtn"
          v-if="dataForm.state == 5 || dataForm.state == 8"
          v-loading.fullscreen.lock="loadingrejected"
          >驳回</el-button
        >
      </div>
      <el-row>
        <el-col :span="24">
          <el-table
            style="width: 100%; margin-top: 20px"
            :data="dataList"
            border
            ref="table"
            v-loading="loading"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column
              type="index"
              width="80"
              header-align="center"
              align="center"
              label="序号"
            ></el-table-column>
            <el-table-column
              prop="batchNumber"
              width="60"
              header-align="center"
              align="center"
              label="批次"
            ></el-table-column>
            <el-table-column
              prop="renwuName"
              header-align="center"
              align="center"
              label="任务名称"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-input
                  v-if="!disabled"
                  v-model="scope.row.renwuName"
                  placeholder="任务名称"
                />
                <span v-else>{{ scope.row.renwuName }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="难度系数"
              header-align="center"
              align="center"
            >
              <el-table-column
                prop="zycd"
                header-align="center"
                align="center"
                label="重要程度"
              >
                <template slot="header" slot-scope="scope">
                  <span style="margin-right: 15px">重要程度</span>
                  <el-tooltip class="item" effect="dark" placement="top-start">
                    <div slot="content">
                      说明：<br />
                      1. 重要程度高，是指重点任务对集团层面有重要影响；<br />
                      2. 重要程度较高，是指重点任务对公司层面有重要影响；<br />
                      3. 重要程度一般，是指重点任务对部门层面有重要影响；<br />
                    </div>
                    <i class="el-icon-info" style="color: #3e51a3"></i>
                  </el-tooltip>
                </template>
                <template slot-scope="scope">
                  <el-select
                    v-if="!disabled"
                    v-model="scope.row.zycd"
                    placeholder="请选择重要程度"
                  >
                    <el-option
                      v-for="(item, index) in chengduList"
                      :label="item.type"
                      :value="item.id"
                      :key="index"
                    ></el-option>
                  </el-select>
                  <span v-else>{{ scope.row.zycd }}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="wcnd"
                header-align="center"
                align="center"
                label="完成难度"
              >
                <template slot="header" slot-scope="scope">
                  <span style="margin-right: 15px">完成难度</span>
                  <el-tooltip class="item" effect="dark" placement="top-start">
                    <div slot="content">
                      说明：<br />
                      1. 完成难度高，是指重点任务创新性强、协调难度大；<br />
                      2.完成难度较高，是指重点任务有一定创新性、需要争取政策支持；<br />
                      3.完成难度一般，是指重点任务有一定难度，但有专业团队支持，本部门能够独立承担工作；<br />
                    </div>
                    <i class="el-icon-info" style="color: #3e51a3"></i>
                  </el-tooltip>
                </template>
                <template slot-scope="scope">
                  <el-select
                    v-if="!disabled"
                    v-model="scope.row.wcnd"
                    placeholder="请选择完成难度"
                  >
                    <el-option
                      v-for="(item, index) in chengduList"
                      :label="item.type"
                      :value="item.id"
                      :key="index"
                    ></el-option>
                  </el-select>
                  <span v-else>{{ scope.row.wcnd }}</span>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              label="任务有效期"
              min-width="400"
            >
              <el-table-column
                prop="rwYxqq"
                align="center"
                label="开始时间"
                width="200"
              >
                <template slot-scope="scope">
                  <el-date-picker
                    v-if="!disabled"
                    type="date"
                    placeholder="选择日期"
                    v-model="scope.row.rwYxqq"
                    style="width: 100%"
                    value-format="yyyy-MM-dd"
                    :pickerOptions="startTimPickerOptions"
                    @focus="handleFocus(scope.row.rwYxqz)"
                  ></el-date-picker>
                  <span v-else>{{ scope.row.rwYxqq }}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="rwYxqz"
                align="center"
                label="结束时间"
                width="200"
              >
                <template slot-scope="scope">
                  <el-date-picker
                    v-if="!disabled"
                    type="date"
                    placeholder="选择日期"
                    v-model="scope.row.rwYxqz"
                    style="width: 100%"
                    value-format="yyyy-MM-dd"
                    :pickerOptions="endTimPickerOptions"
                    @focus="handleFocus(scope.row.rwYxqq)"
                  ></el-date-picker>
                  <span v-else>{{ scope.row.rwYxqz }}</span>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column
              prop="khrName"
              header-align="center"
              align="center"
              label="考核人"
            >
              <template slot-scope="scope">
                <div v-if="!disabled">
                  <span v-if="scope.row.khrName">{{scope.row.khrName}}</span>
                  <i class="el-icon-edit-outline" style="margin-left: 10px;" @click="chooseBtn(scope.row)"></i>
                </div>
                <div v-else>
                  <span v-if="scope.row.khrName">{{scope.row.khrName}}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="weightset"
              header-align="center"
              align="center"
              label="权重"
              width="90"
            >
              <template slot-scope="scope">
                <el-input
                  v-if="!disabled"
                  type="number"
                  min="0"
                  max="100"
                  oninput="value=value.replace(/[^\d]/g,0);if(isNaN(value))value=0;if(value>100)value=100;
                                      if(value<0)value=0;if(value.charAt(0)==0&&value.charAt(1)!='')value=0"
                  v-model="scope.row.weightset"
                  placeholder="请输入内容"
                />
                <span v-else>{{ scope.row.weightset }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="workTarget"
              header-align="center"
              align="center"
              label="工作目标/成效"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-input
                  v-if="!disabled"
                  v-model="scope.row.workTarget"
                  placeholder="工作目标/成效"
                />
                <span v-else>{{ scope.row.workTarget }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="auditStatusDsc"
              header-align="center"
              align="center"
              width="120"
              label="状态"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              width="60"
              header-align="center"
              align="center"
              v-if="!disabled"
            >
              <template slot="header">
                <el-button
                  type="primary"
                  icon="el-icon-plus"
                  circle
                  @click="addBtn()"
                  :disabled="dataForm.userLx == 1"
                ></el-button>
              </template>
              <template slot-scope="scope">
                <el-button
                  type="danger"
                  icon="el-icon-minus"
                  circle
                  @click="delBtn(scope.row)"
                  :disabled="dataForm.userLx == 1"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" plain @click="saveBtn" v-loading.fullscreen.lock="fullscreenLoading">保存</el-button>
        <el-button type="primary" plain @click="backBtn">关闭</el-button>
      </span>
    </el-dialog>
    <UserCommonSelect ref="usercomment" @submitfy="submitfy"></UserCommonSelect>
    <sortingPerChooseDialog
      :sortingPerChooseDialogData="sortingPerChooseDialogData"
      @sortingPerChooseDialogChild="sortingPerChooseDialogChild"
    ></sortingPerChooseDialog>
  </div>
</template>

<script>
var clickStartTime = "";
import UserCommonSelect from "@/views/hetongrenwu/components/usercomment-select";
import sortingPerChooseDialog from "@/views/hetongrenwu/components/sortingPerChooseDialog.vue";
import {
  getDetailListdepid,
  removeDetailById,
  rejected,
  updateForFaqi,
  saveDetailForCj
} from "@/api/hetongrenwu/zhongdian/renwuchuangjian";
import { effective } from "@/api/hetongrenwu/hetongyeji/hetongnd";
import {getList} from '@/api/hetongrenwu/zhongdian/renyuanfenzu'
export default {
  components: {
    UserCommonSelect,
    sortingPerChooseDialog,
  },
  props: {},
  data() {
    var checkDate = (rule, value, callback) => {
      if (value) {
        if (
          new Date(value.replace(/-/g, "/")) <
          new Date(this.dataForm.rwYxqq.replace(/-/g, "/"))
        ) {
          callback("结束日期不能小于开始日期!");
        } else {
          callback();
        }
      } else {
        callback("请选择结束日期");
      }
    };
    var checkDateKai = (rule, value, callback) => {
      if (value) {
        if (
          new Date(value.replace(/-/g, "/")) >
          new Date(this.dataForm.rwYxqz.replace(/-/g, "/"))
        ) {
          callback("开始日期不能大于结束日期!");
        } else {
          callback();
        }
      } else {
        callback("请选择开始日期");
      }
    };
    return {
      endTimPickerOptions: {
        disabledDate(time, a, b) {
          var startDate = new Date(clickStartTime);
          return time.getTime() < startDate.getTime();
        },
      },
      startTimPickerOptions:{
        disabledDate(time, a, b) {
          var startDate = new Date(clickStartTime);
          return time.getTime() > startDate.getTime();
        },
      },
      dataForm: {},
      shouyuerendataList: [], 
      categoryList: [],
      chengduList: [],
      dataList: [],
      zdrwBkhUsers: [],
      dialogVisible: false,
      index: 1,
      state: Number,
      loading: false,
      loadingrejected: false,
      disabled: Boolean,
      dataRule: {
        faqiName: [
          { required: true, message: "请输入发起名称", trigger: "change" },
        ],
        rwYxqq: [{ validator: checkDateKai, trigger: "change" }],
        rwYxqz: [{ validator: checkDate, trigger: "change" }],
        signDate: [
          { required: true, message: "请选择填报结束日期", trigger: "change" },
        ],
        templateId: [
          { required: true, message: "请选择模板名称", trigger: "change" },
        ],
        userLx: [
          { required: true, message: "请选择发起类型", trigger: "change" },
        ],
      },
      sortingPerChooseDialogData:{},
      fullscreenLoading:false,
    };
  },
  watch: {},
  created() {
    this.fneffective();
  },
  methods: {
    init(row) {
      this.dataForm = row;
      this.dialogVisible = true;
      this.fngetDetailListdepid(row.recordId);
      this.getxList();
      
      if (row.state <= 12 || row.state ==711) {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
    },
    getxList(){
      getList().then(res=>{
        this.chengduList = res.data.data;
      })
    },
    saveBtn(){
      var data = this.dataList;
      var params ={
        cj_rows:this.dataForm,
        rows:data,
      }
      for(var i=0;i<data.length;i++){
        if(data[i].khrId == ''){
          this.$message.warning('第'+[i+1]+'行请选择考核人')
          return
        }
      }
      this.fullscreenLoading = true;
      saveDetailForCj(params).then(res=>{
        this.$message.success("保存成功");
        this.fullscreenLoading = false;
        this.backBtn();
      }).catch(res=>{
        this.fullscreenLoading = false;
      })
    },
    backBtn() {
      this.dialogVisible = false;
      this.$emit("fnBackLastPage", "返回原页面");
    },
    changeHead(val, type, id){
      var param ={
        [type]: val,
        id: id,
      }
      updateForFaqi(param).then((res) => {
        this.$message.closeAll();
        this.$message.success("修改成功");
        this.fngetDetailListdepid(this.dataForm.recordId);
      });
    },
    fneffective() {
      effective({
        tagName: "重点任务",
      }).then((res) => {
        this.categoryList = res.data.data;
      });
    },
    fngetDetailListdepid(id) {
      this.loading = true;
      getDetailListdepid({ id }).then((response) => {
        this.dataList = response.data.data;
        var indexs = 0
        this.dataList.forEach(ele=>{
          ele.index =indexs;
          indexs++;
        })
        this.loading = false;
      });
    },
    rejectedBtn() {
      var data = this.$refs.table.selection;
      this.loadingrejected = true;
      rejected({ rows: data }).then((res) => {
        this.$message.success("驳回成功");
        this.loadingrejected = false;
        this.fngetDetailListdepid(this.dataForm.recordId);
      });
    },
    submitfy(data) {
      this.zdrwBkhUsers = [];
      this.shouyuerendataList = [];
      this.shouyuerendataList = data.tableDataUser;
      this.shouyuerendataList.forEach((ele) => {
        var user = {};
        user.userId = ele.userId;
        user.userName = ele.userCode;
        user.ryName = ele.userName;
        user.deptId = ele.deptId;
        user.userDeptName = ele.deptName;
        user.userZw = ele.userZw;
        user.userLx = "总部发起";
        user.userZj = ele.userZj;
        this.zdrwBkhUsers.push(user);
      });
    },
    handleFocus(startTime) {
      clickStartTime = startTime;
    },
    delBtn(row) {
      if(row.id){
        removeDetailById(row.id).then((res) => {});
      }else{
        this.dataList.forEach((eles, i) => {
          if (row.index == eles.index) {
            this.dataList.splice(i, 1);
          }
        });
      }
    },
    addBtn() {
      var item = {
        renwuName: "任务名称" + this.index + 1,
        bkhName: "1",
        zycd: this.chengduList[0].id,
        wcnd: this.chengduList[0].id,
        weightset: "0",
        rwYxqq: this.dataForm.rwYxqq,
        rwYxqz: this.dataForm.rwYxqz,
        workTarget: "无",
        dataProvider: "1",
        providerName: "1",
        auditStatusDsc:"填报中",
        auditStatus:0,
        index: this.index,
        khrName:"",
        khrId:"",
        ndId: this.dataForm.id,
        renwuId: this.dataForm.renwuId,
      };
      this.index++;
      this.dataList.push(item)
    },
    chooseBtn(row){
      this.sortingPerChooseDialogData={
        visible: true,
        types:"zdrw",
        perLsit:{
          userName:row.khrName,
          userId:row.khrId,
        },
        index:row.index
      }
    },
    sortingPerChooseDialogChild(data){
      this.dataList.forEach(ele=>{
        if(data.index == ele.index){
          ele.khrName = data.khrName,
          ele.khrId = data.khrId
        }
      })
    },
  },
};
</script>

<style scoped>
.rwfqnew {
  border-radius: 16px;
  -webkit-box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  background: #fbfbfd;
  padding: 10px 30px 30px 30px;
}
</style>
